package com.socket.nio.nio.bytebuf;

import com.socket.nio.nio.util.ByteBufPrinter;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/**
 * @author Hexudong
 * @date 2025/4/13
 */
public class TestSlice {
  public static void main(String[] args) {
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10);
    buf.writeBytes(new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'});
    ByteBufPrinter.prettyPrint(buf);

    // 切片过程中没有发生数据复制
    ByteBuf b1 = buf.slice(0, 5);
    ByteBuf b2 = buf.slice(5, 5);
    //ByteBufPrinter.prettyPrint(b1);
    //ByteBufPrinter.prettyPrint(b2);

    System.out.println("===================================================================================================");
    // 验证 b1 和 buf 是同一块内存
    b1.setByte(0, 'M');
    ByteBufPrinter.prettyPrint(buf);
    ByteBufPrinter.prettyPrint(b1);
  }
}
